package com.xinqi.modules.databank.v2.service.assistant.impl;

import com.alibaba.fastjson2.JSON;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.enums.AssistantMessageTypeEnum;
import com.xinqi.common.core.enums.ClassRoleEnum;
import com.xinqi.common.core.enums.HomeWorkTypeEnum;
import com.xinqi.modules.chat.chat.official.dto.req.ChatAssistantContentCreateDTO;
import com.xinqi.modules.chat.chat.official.json.BaseMessage;
import com.xinqi.modules.chat.chat.official.json.DataMessage;
import com.xinqi.modules.course.clazz.client.ClassStudentClient;
import com.xinqi.modules.course.clazz.dto.rsp.ClassGroupStudentResultDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassStudentResultDTO;
import com.xinqi.modules.databank.v2.convert.AssistantConvert;
import com.xinqi.modules.databank.v2.domain.CourseResourceEntity;
import com.xinqi.modules.databank.v2.manager.CourseResourceBaseManager;
import com.xinqi.modules.databank.v2.service.assistant.AssistantDataService;
import com.xinqi.modules.databank.v2.service.assistant.AssistantListen;
import jodd.bean.BeanCopy;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AssistantDataServiceImpl implements AssistantDataService {

    private final CourseResourceBaseManager courseResourceBaseManager;

    private final ClassStudentClient classStudentClient;

    private final AssistantConvert assistantConvert;

    @Lazy
    private final AssistantListen assistantListen;

    /**
     * 资料发布消息体
     */
    @Override
    public List<ChatAssistantContentCreateDTO> sentAssistantData(Long contentId) {

        CourseResourceEntity entity = courseResourceBaseManager.getById(contentId);

        List<ClassStudentResultDTO> students = this.getClassStudentList(entity);
        List<ChatAssistantContentCreateDTO> list = new ArrayList<>();

        ChatAssistantContentCreateDTO initCreate = assistantListen.initChatAssistantContent(AssistantMessageTypeEnum.DATA);

        DataMessage initMessage = initDataMessage(entity);

        students.forEach(item -> {
            ChatAssistantContentCreateDTO create = assistantConvert.create(initCreate);
            BeanCopy.beans(initCreate, create);
            create.setContentId(entity.getId());
            DataMessage message = assistantConvert.create(initMessage);
            message.setParticipateNum(0);
            message.setNotParticipateNum(0);
            message.setClassRole(item.getClassRole());
            create.setMessageContext(JSON.toJSONString(message));
            create.setRecipient(item.getStudentId());
            list.add(create);
        });
        return list;
    }

    /**
     * 根据作业类型获取要发送学生列表
     */
    private List<ClassStudentResultDTO> getClassStudentList(CourseResourceEntity entity) {
        // 获取班级所有成员信息
        List<ClassStudentResultDTO> students = R.as(classStudentClient.allUserListByClassId(entity.getClassId()));
        // 获取老师以及关系用户信息
        return students.stream().filter(student -> Objects.equals(YesNoNumberEnum.YES.getCode(), student.getIsRelationalStudent())
            || Objects.equals(YesNoNumberEnum.YES.getCode(), student.getIsTeacher())).collect(Collectors.toList());
    }

    /**
     * 新奇助手初始化发布资料信息
     */
    private DataMessage initDataMessage(CourseResourceEntity entity){
        // 初始化消息体
        BaseMessage base = assistantListen.initBaseMessage(entity.getClassId());
        DataMessage message = JSON.parseObject(JSON.toJSONString(base), DataMessage.class);
        message.setContentId(entity.getId());
        message.setTitle(entity.getTitle());
        message.setEndTime(entity.getEndTime());
        return message;
    }

}
